define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'cookie'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'mall/oper/area/index',
                    add_url: 'mall/oper/area/add',
                    edit_url: 'mall/oper/area/edit',
                    del_url: 'mall/oper/area/del',
                    multi_url: 'mall/oper/area/multi',
                    table: 'area',
                }
            });

            var table = $("#table");

            //加载子节点
            function getChild(treeid) {
                //alert(treeid)
                Fast.api.ajax({
                    type:'get',
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                    data: {"id": treeid},
                    success:function (data,status) {
                        var rows = data.rows;
                        table.bootstrapTable("append", rows);
                        //func();
                        Layer.closeAll();

                    }
                });
            };

            require(['bt-treegrid','jq-treegrid'], function () {
                // 初始化表格
                table.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                    pk: 'id',
                    striped: true,
                    pagination: false,
                    idField: 'id',
                    uniqueId:"id",
                    showColumns: true,
                    columns: [
                        {checkbox: true},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {field: 'id', title: __('ID'), visible: false},
                        {field: 'pid', title: __('Pid'), visible: false},
                        {field: 'level_text', title: __('Level')},
                        //{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        {field: 'operate', title: __('Operate'), table: table,
                            buttons: [{
                                name: 'edit',
                                icon: 'fa fa-pencil',
                                title: __('Edit'),
                                extend: 'data-toggle="tooltip"',
                                classname: 'btn btn-xs btn-success btn-editone btn-dialog',
                                url: $.fn.bootstrapTable.defaults.extend.edit_url
                            },{
                                name: 'del',
                                title: __('Del'),
                                classname: 'btn btn-xs btn-danger btn-ajax',
                                icon: 'fa fa-trash',
                                url: $.fn.bootstrapTable.defaults.extend.del_url,
                                visible: function( row){
                                    //叶节点可以删除
                                    return row.level > 2;
                                },
                                confirm: '确定删除此叶节点?',
                                success: function( row){
                                    table.bootstrapTable("remove", {field:"id",values: row.id})
                                }
                            }],
                            formatter: Table.api.formatter.buttons
                        }
                    ],
                    treeShowField: 'name',
                    parentIdField: 'pid',
                    onPostBody: function () {
                        var columns = table.bootstrapTable('getOptions').columns

                        if (columns && columns[0][1].visible) {
                            table.treegrid({
                                treeColumn: 1,
                                // 核心选项，用于展示的数据
                                //data:getRoot(),
                                //getChild为点击懒加载节点目录时，运行的函数名称，把后端的数据添加到这个节点下面
                                lazyLoad:getChild,
                                onChange: function () {
                                    table.bootstrapTable('resetWidth')
                                }
                            })
                        }
                    }
                })
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $(document).on('click', '.btn-add', function () {
                var ids = Table.api.selectedids(table);
                var url = $.fn.bootstrapTable.defaults.extend.add_url;
                if (url.indexOf("{ids}") !== -1) {
                    url = Table.api.replaceurl(url, {ids: ids.length > 0 ? ids.join(",") : 0}, table);
                }
                Fast.api.open(url, __('Add'), $(this).data() || {});
            });

            $(document).on('click', '.btn-allexpand', function () {
                table.treegrid("expandAll");
            });

            $(document).on('click', '.btn-allcollapse', function () {
                table.treegrid("collapseAll");

            });

        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
        }
    };
    return Controller;
});